import traceback
from django.http import JsonResponse
from django.views.generic import View
from ...models.basicDimensionModel import *
from ...serializers.basicDimensionSerializer import *
from ...utils.common_util.basic_util import get_model_serializer_config
from ...config.basic_config import type_conversion_dict
from ...config.message_config import Config_message_type, Config_message, Error_message_type
from utils.responseContentUtil import *


class BasicDimensionConfigView(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = BasicDimensionListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(BasicDimensionListSerializer)
            table_extra_fields_info = BasicDimensionListSerializer.get_extra_fields_info()
            for extra_fields in table_extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            table_fields.append(
                {"title": "操作", "dataIndex": "operate", "type": "operate"})
            hidden_fields = ["uuid"]
            sortable_fields = []
            link_fields = ["basic_dimension_name"]
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
                if field["dataIndex"] in link_fields:
                    field["isLink"] = True
            data["table_fields"] = table_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)

class BasicDimensionCreateConfigView(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            create_fields = []
            create_fields = get_model_serializer_config(BasicDimensionCreateSerializer)
            for field in create_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["create_fields"] = create_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)

class BasicDimensionModifyInfoConfigView(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            modify_fields = []
            modify_fields = get_model_serializer_config(BasicDimensionModifyInfoSerializer)
            for field in modify_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["modify_fields"] = modify_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)

class BasicDimensionInfoConfigView(View):
    def get(self,request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            info_fields = []
            info_fields = get_model_serializer_config(BasicDimensionInfoSerializer)
            for field in info_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["info_fields"] = info_fields
            return make_get_success_response(data=data)
        except Exception as e:
            traceback.print_exc()
            return make_server_error_response(message=str(e), data=None)
